<?php
// error on this http://weekdodev.com/_test/playground/port/getbrick.php?bx=14&by=29
//http://weekdodev.com/_test/playground/port/getbrick.php?bx=25&by=20
require_once __dir__.'/../common.php';

$bx = $_REQUEST['bx'];
$by = $_REQUEST['by'];


$image = imagecreatefromgif('worldmap.gif');
/*for ($x=0; $x<imagesx($image); $x++)
{
	$rgb = imagecolorsforindex($image, imagecolorat($image, $x, 0));
	echo $rgb['green']==255 ? 1: 0;
}
exit;
*/

$image_temp = imagecreatetruecolor(CELL_COUNT_OF_BRICK_SIDE, CELL_COUNT_OF_BRICK_SIDE);
imagecopyresized ($image_temp, $image, 0, 0, $bx*CELL_COUNT_OF_BRICK_SIDE, $by*CELL_COUNT_OF_BRICK_SIDE,
		CELL_COUNT_OF_BRICK_SIDE, CELL_COUNT_OF_BRICK_SIDE,
		CELL_COUNT_OF_BRICK_SIDE, CELL_COUNT_OF_BRICK_SIDE);

$image_p = imagecreatetruecolor(CELL_COUNT_OF_BRICK_SIDE*CELL_SIZE, CELL_COUNT_OF_BRICK_SIDE*CELL_SIZE);
imagecopyresized ($image_p, $image_temp, 0, 0, 0, 0,
		CELL_COUNT_OF_BRICK_SIDE*CELL_SIZE, CELL_COUNT_OF_BRICK_SIDE*CELL_SIZE,
		CELL_COUNT_OF_BRICK_SIDE, CELL_COUNT_OF_BRICK_SIDE);

$image_brick = imagecreatefromjpeg('white_brick.jpg');
$outside_color = imagecolorallocate($image_p, 200, 200, 200);
$block_images = get_block_images();
for ($x=0; $x<CELL_COUNT_OF_BRICK_SIDE; $x++)
{
	for ($y=0; $y<CELL_COUNT_OF_BRICK_SIDE; $y++)
	{
		$map_x = $bx*CELL_COUNT_OF_BRICK_SIDE+$x;
		$map_y = $by*CELL_COUNT_OF_BRICK_SIDE+$y;
		
		if ($map_x<0 || $map_y<0 || $map_x>=imagesx($image) || $map_y>=imagesy($image))
		{
			imagefilledrectangle($image_p, CELL_SIZE*$x, CELL_SIZE*$y,
				CELL_SIZE+CELL_SIZE*$x, CELL_SIZE+CELL_SIZE*$y, $outside_color);
			continue;
		}
		
		$rgb = imagecolorsforindex($image,
			imagecolorat($image, $map_x, $map_y));

		if ($block_images[$map_x][$map_y])
		{
			$item = $block_images[$map_x][$map_y];
			$item = item::appendImageRealUrl($item);

			$resizeImage = new ResizeImage();
			if (!$resizeImage) continue;
			
			$resizeImage->load($_SERVER['DOCUMENT_ROOT'].$item['item_image_s']);
			$resizeImage->autoFullFit(CELL_SIZE, CELL_SIZE);
			$im = $resizeImage->image;

			imagecopyresized ($image_p, $im, CELL_SIZE*$x, CELL_SIZE*$y,
					0, 0, CELL_SIZE, CELL_SIZE,
					imagesx($im), imagesy($im));
		}
		else if (array_sum($rgb) == 255*3) // only white is empty land
		{
			imagecopyresized ($image_p, $image_brick, CELL_SIZE*$x, CELL_SIZE*$y,
					0, 0, CELL_SIZE, CELL_SIZE,
					imagesx($image_brick), imagesy($image_brick));
		}
	}
}


/*$image_ore = imagecreatefromjpeg('ore.jpg');
for ($x=0; $x<CELL_COUNT_OF_BRICK_SIDE; $x++)
{
	for ($y=0; $y<CELL_COUNT_OF_BRICK_SIDE; $y++)
	{
		$type = r($bx*CELL_SIZE+$x, $by*CELL_SIZE+$y);
		if ($type < 1)
		{
			imagecopyresized ($image_p, $image_ore, $x*CELL_SIZE, $y*CELL_SIZE, 0, 0,
				imagesx($image_ore), imagesx($image_ore), imagesx($image_ore), imagesy($image_ore));
			
		}
	}
}

function r($x, $y)
{
	$r = (($x*9999+$y)* 16807) % ((1 << 31) - 1);
	return $r%10;
}
*/

header('Content-type: image/jpeg');
imagejpeg($image_p, null, 80);

// should be optimized
function get_block_images()
{
	$ret_arr = array();

	$result = sys::sql("select * from test_items");
	if (empty($result)) return false;

	foreach ($result as $v)
	{
		$ret_arr[$v['item_x']][$v['item_y']] = array(
				'item_id' => $v['item_id'],
				'item_image' => $v['item_image'],
				);
	}
	return $ret_arr;
}

function get_image_from_file($filename)
{
	$image_info = getimagesize($filename);
	if (empty($image_info) || !$image_info[2])
		return false;
	
	$image_type = $image_info[2];
	if( $image_type == IMAGETYPE_JPEG ) {
		return imagecreatefromjpeg($filename);
	} elseif( $image_type == IMAGETYPE_GIF ) {
		return imagecreatefromgif($filename);
	} elseif( $image_type == IMAGETYPE_PNG ) {
		return imagecreatefrompng($filename);
	}
}
